/*
 * Skybox.cpp
 *
 *  Created on: 2012/07/06
 *      Author: weihe
 */

#include "Skybox.h"
#include <gl/glut.h>

namespace object {

const float DX = 1.0 / 3.0;
const float DY = 0.5;

void Skybox::draw() {
	// Store the current matrix
	glPushMatrix();
	{

		// Reset and transform the matrix.
		glLoadIdentity();
		gluLookAt(0, 0, 0, camera->frontVector().x, camera->frontVector().y,
				camera->frontVector().z, camera->upVector().x, camera->upVector().y,
				camera->upVector().z);
		glScalef(-1, 1, 1);
		// Enable/Disable features
		glPushAttrib(GL_ENABLE_BIT);
		{
			glEnable(GL_TEXTURE_2D);
			glDisable(GL_DEPTH_TEST);
			glDisable(GL_LIGHTING);
			glDisable(GL_BLEND);

			// Just in case we set all vertices to white.
			glColor4f(1, 1, 1, 1);

			// Render the front quad
			textures->bind(0);
			glBegin(GL_QUADS);
			{
				glTexCoord2f(0, 0);
				glVertex3f(-0.5f, 0.5f, 0.5f);
				glTexCoord2f(1, 0);
				glVertex3f(0.5f, 0.5f, 0.5f);
				glTexCoord2f(1, 1);
				glVertex3f(0.5f, -0.5f, 0.5f);
				glTexCoord2f(0, 1);
				glVertex3f(-0.5f, -0.5f, 0.5f);
			}
			glEnd();

			// Render the left quad
			textures->bind(2);
			glBegin(GL_QUADS);
			{
				glTexCoord2f(0, 0);
				glVertex3f(-0.5f, 0.5f, -0.5f);
				glTexCoord2f(1, 0);
				glVertex3f(-0.5f, 0.5f, 0.5f);
				glTexCoord2f(1, 1);
				glVertex3f(-0.5f, -0.5f, 0.5f);
				glTexCoord2f(0, 1);
				glVertex3f(-0.5f, -0.5f, -0.5f);

			}
			glEnd();

			// Render the back quad
			textures->bind(1);
			glBegin(GL_QUADS);
			{
				glTexCoord2f(0, 0);
				glVertex3f(0.5f, 0.5f, -0.5f);
				glTexCoord2f(1, 0);
				glVertex3f(-0.5f, 0.5f, -0.5f);
				glTexCoord2f(1, 1);
				glVertex3f(-0.5f, -0.5f, -0.5f);
				glTexCoord2f(0, 1);
				glVertex3f(0.5f, -0.5f, -0.5f);
			}
			glEnd();

			// Render the right quad
			textures->bind(3);
			glBegin(GL_QUADS);
			{
				glTexCoord2f(0, 0);
				glVertex3f(0.5f, 0.5f, 0.5f);
				glTexCoord2f(1, 0);
				glVertex3f(0.5f, 0.5f, -0.5f);
				glTexCoord2f(1, 1);
				glVertex3f(0.5f, -0.5f, -0.5f);
				glTexCoord2f(0, 1);
				glVertex3f(0.5f, -0.5f, 0.5f);
			}
			glEnd();

			// Render the top quad
			textures->bind(4);
			glBegin(GL_QUADS);
			{
				glTexCoord2f(0, 0);
				glVertex3f(-0.5f, 0.5f, -0.5f);
				glTexCoord2f(1, 0);
				glVertex3f(0.5f, 0.5f, -0.5f);
				glTexCoord2f(1, 1);
				glVertex3f(0.5f, 0.5f, 0.5f);
				glTexCoord2f(0, 1);
				glVertex3f(-0.5f, 0.5f, 0.5f);

			}
			glEnd();

			if (size == 6) {
				// Render the bottom quad
				textures->bind(5);
				glBegin(GL_QUADS);
				{
					glTexCoord2f(0, 0);
					glVertex3f(-0.5f, -0.5f, 0.5f);
					glTexCoord2f(1, 0);
					glVertex3f(0.5f, -0.5f, 0.5f);
					glTexCoord2f(1, 1);
					glVertex3f(0.5f, -0.5f, -0.5f);
					glTexCoord2f(0, 1);
					glVertex3f(-0.5f, -0.5f, -0.5f);
				}
				glEnd();
			}

			// Restore enable bits and matrix
		}
		glPopAttrib();
	}
	glPopMatrix();
}

} /* namespace object */

